<template>
  <div class="page-box">
    <el-card class="page-box-card">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        label-width="88px"
        inline
        @submit.native.prevent
      >
        <el-form-item label="身份证" prop="param.idCard">
          <el-input
            v-model="queryParams.param.idCard"
            placeholder="请输入身份证"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="姓名" prop="param.name">
          <el-input
            v-model="queryParams.param.name"
            placeholder="请输入姓名"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="性别" prop="param.sex">
          <el-select v-model="queryParams.param.sex" placeholder="请选择性别">
            <el-option
              v-for="item in dict.type.sys_user_sex"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据来源" prop="param.source">
          <el-select
            v-model="queryParams.param.source"
            placeholder="请选择数据来源"
          >
            <el-option
              v-for="item in dict.type.data_source"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['definition:class:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleEdit"
            v-hasPermi="['definition:class:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['definition:class:delete']"
            >删除</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload2"
            size="mini"
            v-hasPermi="['definition:class:upload']"
            >导入</el-button
          >
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['definition:class:download']"
            >导出</el-button
          >
        </el-col>
      </el-row>
      <el-table
        :data="dataList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
        />
        <el-table-column type="index" label="序号" width="60" fixed="left" />
        <el-table-column
          prop="idCard"
          label="身份证号"
          width="240"
          fixed="left"
        />
        <el-table-column prop="name" label="姓名" width="180" />
        <el-table-column prop="sex" label="性别" width="180">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_user_sex"
              :value="scope.row.sex"
            />
          </template>
        </el-table-column>
        <el-table-column prop="sourceTime" label="建档时间" width="180" />
        <el-table-column prop="sourceCompany" label="建档单位" />
        <el-table-column prop="sourceBy" label="建档员" />
        <el-table-column prop="source" label="数据来源">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.data_source"
              :value="scope.row.source"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="240">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              style="color: red"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >删除</el-button
            >
            <el-button
              disabled
              size="mini"
              type="text"
              icon="el-icon-thumb"
              @click="handleDetail(scope.row, '0')"
              v-hasPermi="['system:dept:edit']"
              >查看筛查档案</el-button
            >
            <el-button
              disabled
              size="mini"
              type="text"
              icon="el-icon-thumb"
              @click="handleDetail(scope.row, '1')"
              v-hasPermi="['system:dept:edit']"
              >查看急救档案</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-thumb"
              @click="handleDetail(scope.row, '2')"
              v-hasPermi="['system:dept:edit']"
              >查看治疗档案</el-button
            >
            <el-button
              disabled
              size="mini"
              type="text"
              icon="el-icon-thumb"
              @click="handleDetail(scope.row, '3')"
              v-hasPermi="['system:dept:edit']"
              >查看康复档案</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="addDialog"
      width="1200px"
      append-to-body
    >
      <el-form ref="form" :model="addForm" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idCard">
              <el-input
                v-model="addForm.idCard"
                placeholder="请输入身份证号"
                @blur="getInfo"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input
                v-model="addForm.name"
                placeholder="请输入姓名"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select
                v-model="addForm.sex"
                placeholder="请选择性别"
                style="width: 100%"
              >
                <el-option
                  v-for="item in dict.type.sys_user_sex"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value - 0"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birthDate">
              <el-date-picker
                placeholder="请选择"
                v-model="addForm.birthDate"
                style="width: 100%"
                value-format="yyyy-MM-dd"
                type="date"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="民族" prop="nation">
              <el-input
                v-model="addForm.nation"
                placeholder="请输入民族"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建档年龄">
              <el-input
                v-model="addForm.createAge"
                placeholder="请输入建档年龄"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="户籍地市">
              <el-input
                v-model="addForm.registeredCity"
                placeholder="请输入户籍地市"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="户籍详细地址">
              <el-input
                v-model="addForm.registeredCityDetail"
                placeholder="请输入户籍详细地址"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="户籍地址邮编">
              <el-input
                v-model="addForm.postalCode"
                type="number"
                placeholder="请输入户籍地址邮编"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建档时间" prop="sourceTime">
              <el-date-picker
                placeholder="请选择"
                v-model="addForm.sourceTime"
                style="width: 100%"
                value-format="yyyy-MM-dd"
                type="date"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="建档单位" prop="sourceCompany">
              <el-input
                v-model="addForm.sourceCompany"
                placeholder="请输入建档单位"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建档员" prop="sourceBy">
              <el-input
                v-model="addForm.sourceBy"
                placeholder="请输入建档员"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="数据来源" prop="projectNum">
              <el-select v-model="sourceMap" @change="changeSource" placeholder="请选择数据来源" style="width: 100%">
                <el-option v-for="item in dict.type.data_source" :key="item.value" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  insertPmsPatient,
  updatePmsPatient,
  deletePmsPatient,
  listPmsPatient,
  selectPmsPatientByIdCard,
  exportData,
} from "@/api/data/population";
export default {
  dicts: ["data_source", "sys_user_sex"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      selectionData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        param: {
          idCard: "",
          name: "",
          sex: "",
          source: "",
        },
      },
      sourceMap: "",
      props: {
        checkStrictly: true,
        label: "name",
        value: "id",
      },
      dataList: [],
      total: 0,
      treeData: [],
      title: "",
      addDialog: false,
      addForm: {
        name: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
          },
        ],
        idCard: [
          {
            required: true,
            message: "请输入身份证号",
            trigger: "blur",
          },
          {
            validator: (rule, value, callback) => {
              if (!/^\d{17}[\dXx]|\d{15}$/.test(value)) {
                callback(new Error("请输入正确的身份证号"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeSource(value) {
      this.addForm.source = value;
    },
    // 导出
    handleExport() {
      const params = {
        ...this.queryParams,
      };
      // const idList = [...this.ids];
      this.$modal
        .confirm("是否确认导出当前搜索条件下的人口学信息数据？")
        .then(() => {
          exportData(params)
            .then((res) => {
              // const url = window.URL.createObjectURL(new Blob([res]));
              const blob = new Blob([res]);
              this.$download.saveAs(blob, "人口学信息数据.xlsx");
              this.$message.success("导出成功");
            })
            .catch((error) => {
              this.$message.success("下载Excel失败:", error);
              console.error("下载Excel失败:", error);
            });
        });
    },
    // 查看详情
    handleDetail(row, type) {
      this.$router.push({
        path: "/data/treat",
        query: {
          patientId: row.id,
        },
      });
      // this.$router.push({
      //   path: "/data/archivesDetails",
      //   query: {
      //     id,
      //     type,
      //   },
      // });
    },
    // 新增项目编号查重事件
    getInfo(e) {
      if (this.addForm.id) {
        return;
      }
      const idCard = e.target.value;
      selectPmsPatientByIdCard({
        idCard,
      }).then((res) => {
        console.log("res: ", res);
      });
    },
    // 搜索事件
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置事件
    resetQuery() {
      this.dataList = [];
      this.total = 0;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 获取数据列表
    getList() {
      this.loading = true;
      const param = {
        ...this.queryParams,
      };
      console.log("param: ", param);
      listPmsPatient(param).then((response) => {
        console.log("response: ", response);
        this.dataList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 列表选中事件
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.selectionData = [...selection];
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 新增人口信息
    handleAdd() {
      this.title = "新增人口信息";
      this.addForm = {
        name: "",
        idCard: "",
        sex: "",
        source: "",
        status: "0",
      };
      this.resetForm("form");
      this.sourceMap = "";
      this.addDialog = true;
    },
    // 新增/修改弹框提交事件
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const param = {
            ...this.addForm,
          };
          if (param.id) {
            // 修改
            updatePmsPatient(param).then((res) => {
              this.$message.success("修改成功");
              this.cancel();
              this.handleQuery();
            });
          } else {
            // 新增
            insertPmsPatient(param).then((res) => {
              this.$message.success("新增成功");
              this.cancel();
              this.handleQuery();
            });
          }
        }
      });
    },
    // 新增/修改弹框取消事件
    cancel() {
      this.addForm = {
        name: "",
      };
      this.addDialog = false;
    },
    // 修改人口信息
    handleEdit(row) {
      this.title = "修改人口信息";
      const data = row.id ? row : this.selectionData[0];
      this.addForm = {
        ...data,
      };
      this.dict.type.data_source.forEach((item) => {
        if (item.value == data.source) {
          this.sourceMap = item.label;
        }
      });
      this.addDialog = true;
    },
    // 删除人口信息
    handleDelete(row) {
      const idList = row.id ? [row.id] : [...this.ids];
      this.$modal
        .confirm("是否确认删除当前选择的人口信息？")
        .then(function () {
          // 删除
          return deletePmsPatient({
            idList,
          });
        })
        .then(() => {
          this.$message.success("删除成功");
          this.handleQuery();
        })
        .catch(() => {});
    },
  },
};
</script>
